package cn.mygweb.common.utils;

import java.io.File;

import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;

import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

/** 
* <p>Title: UploadUtils.java</p>
* <p>Description: 文件上传工具类</p>  
* @author gxb 
* @date 2019年6月12日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
public class UploadUtils {
	
	public static final String IMAGES = "images";//图片
	
	public static final String FILES = "files";//文件
	
	public static final String QRCODE = "qrcode";//二维码
	
	public static final String ATTACHMENT = "attachment";//邮件
	
	public static final String FILE_PREFIX = "/files/";//返回的文件夹前缀

	/**
	 * <p>Title: getRelativePath</p>
	 * <p>Description: 通过上传路径获取映射的路径</p>
	 * @param uploadPath 上传的真实路径
	 * @param prefix	  配置文件中设置的上传路径前缀
	 * @return
	 */
	public static String getRelativePath(String uploadPath, String prefix) {
		if(uploadPath != null && !StringUtils.isEmptyOrWhitespace(uploadPath)) {
			return FILE_PREFIX + uploadPath.substring(uploadPath.indexOf(prefix) + prefix.length());
		}
		return null;
	}
	
	/**
	 * <p>Title: uploadImg</p>
	 * <p>Description: 上传图片</p>
	 * @param file
	 * @param uploadFilePath
	 * @return
	 */
	public static String uploadImg(MultipartFile file, String uploadFilePath) {
		return upload(file, uploadFilePath, IMAGES);
	}
	
	/**
	 * <p>Title: uploadFile</p>
	 * <p>Description: 上传文件</p>
	 * @param file
	 * @param uploadFilePath
	 * @return
	 */
	public static String uploadFile(MultipartFile file, String uploadFilePath) {
		return upload(file, uploadFilePath, FILES);
	}
	
	/**
	 * <p>Title: upload</p>
	 * <p>Description: 上传文件</p>
	 * @param file
	 * @param uploadFilePath
	 * @param type
	 * @return	返回文件存储路径的映射路径，参照application.properties文件中的 file.upload.path.relative
	 */
	public static String upload(MultipartFile file, String uploadFilePath, String type) {
		try {
			String fileName = file.getOriginalFilename();
			fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
			//文件夹结构例如：D:/web-upload/images/2019-08-04/1564904895227.png
			String path = uploadFilePath + type + "/" + TimeUtils.currentDay() + "/";
			File filepath = new File(path, fileName);
			if(!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			file.transferTo(new File(path + fileName));
			return FILE_PREFIX + path.substring(path.indexOf(uploadFilePath) + uploadFilePath.length()) + fileName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * <p>Title: uploadReturnRealPath</p>
	 * <p>Description: 上传文件并返回真实的存储路径</p>
	 * @param file
	 * @param uploadFilePath
	 * @param type
	 * @return	真实存储的路径
	 */
	public static String uploadReturnRealPath(MultipartFile file, String uploadFilePath, String type) {
		try {
			String fileName = file.getOriginalFilename();
			fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
			//文件夹结构例如：D:/web-upload/images/2019-08-04/1564904895227.png
			String path = uploadFilePath + type + "/" + TimeUtils.currentDay() + "/";
			File filepath = new File(path, fileName);
			if(!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			file.transferTo(new File(path + fileName));
			return path + fileName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * <p>Title: uploadQrcode</p>
	 * <p>Description: 上传二维码，服务器不做二维码的保留，每次新生成的二维码会覆盖之前的二维码</p>
	 * @param matrix
	 * @param uploadFilePath
	 * @return
	 */
	public static String uploadQrcode(BitMatrix matrix, String uploadFilePath) {
		try {
			String fileName = "qrcode.jpg";
			String path = uploadFilePath + QRCODE + "/";
			File filepath = new File(path, fileName);
			if(!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			MatrixToImageWriter.writeToPath(matrix, "jpg", new File(path + fileName).toPath());
			return FILE_PREFIX + path.substring(path.indexOf(uploadFilePath) + uploadFilePath.length()) + fileName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
